package main

import (
	"crypto/sha256"
	"encoding/hex"
	"fmt"
	"io"
	"os"
)

func main() {
	sha256_test1()
	sha256_test2([]byte("王彦超"))
	sha256_test3([]byte("王彦超"))
}

const fileame_path = `./Service.exe`

func sha256_test1() {
	// 1.打开文件
	file, err := os.Open(fileame_path)
	if err != nil {
		fmt.Println(err)
		return
	}
	// 2.生成sha256hasher 操作类
	hasher := sha256.New()

	// 3.将文件内容写入到hash里面
	lenter, err := io.Copy(hasher, file)
	if err != nil {
		fmt.Println(err)
		return
	}
	fmt.Println("lenter:", lenter)

	// 4.生成hash
	hash := hasher.Sum(nil)
	fmt.Printf("hash: %x\n", hash)
	fmt.Println("hash:", hash[:])
	fmt.Println("hash:", string(hash[:]))
	fmt.Println("hash:", hex.EncodeToString(hash[:]))
	fmt.Println(`---------------------------------------------------`)
}

func sha256_test2(text []byte) {
	hash := sha256.Sum256(text)
	fmt.Printf("hash: %x\n", hash)
	fmt.Println("hash:", string(hash[:]))
	fmt.Println("hash:", hex.EncodeToString(hash[:]))
	fmt.Println(`---------------------------------------------------`)
}

func sha256_test3(text []byte) {
	hasher := sha256.New()
	hasher.Write(text)
	hash:=hasher.Sum(nil)
	fmt.Printf("hash: %x\n", hash)
	fmt.Println("hash:", string(hash[:]))
	fmt.Println("hash:", hex.EncodeToString(hash[:]))
}